package cn.sdormitory.job;

import cn.sdormitory.canteen.dao.FoodHealthCartDao;
import cn.sdormitory.canteen.dao.FoodOrderDao;
import cn.sdormitory.canteen.vo.FoodOrderVO;
import cn.sdormitory.sys.dao.SysNoticeDao;
import cn.sdormitory.sys.entity.SysNotice;
import org.springframework.beans.factory.annotation.Autowired;
import cn.sdormitory.canteen.vo.FoodHealthCartVO;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ExpiredRemindScheduled {

    @Autowired
    private SysNoticeDao sysNoticeDao;
    @Autowired
    private FoodOrderDao foodOrderDao;
    @Autowired
    private FoodHealthCartDao foodHealthCartDao;

    /**
     * 预警提醒
     */
//    @Scheduled(cron = "0 0 9 * * ?")
    public void expiredRemind(){
        //到达收货时间没有收货的订单
        //查询前一天到期并未收货的订单
        List<FoodOrderVO> list = foodOrderDao.getExpiredOrderByYesterday();
        if(list!=null && list.size()>0){
            for(FoodOrderVO foodOrderVO:list){
                SysNotice vo = new SysNotice();
                vo.setNoticeTitle("到期未收货提醒");
                vo.setNoticeStatus("0");
                vo.setCreateBy("system");
                vo.setCreatedTime(new Date());
                StringBuffer sb = new StringBuffer();
                sb.append("订单号:[").append(foodOrderVO.getOrderNo()).append("]");
                sb.append("供应商:[").append(foodOrderVO.getSupplier()).append("]");
                sb.append("的商品还未确认收货，供应商电话：").append(foodOrderVO.getSupplierPhone());
                vo.setNoticeContent(sb.toString());
                sysNoticeDao.insert(vo);
            }
        }
        //食品临近过期库存剩余量提醒
    }

    /**
     * 健康卡到期提醒名单
     */
//    @Scheduled(cron = "0 0 10 * * ?")
    public void healthCartRemind(){
        //获取提前提醒的天数
        Integer day = 30;
        List<FoodHealthCartVO> list = foodHealthCartDao.getOverdueHealthCartList(day);
        SysNotice sysNotice = null;
        for(FoodHealthCartVO vo : list){
            sysNotice = new SysNotice();
            sysNotice.setNoticeTitle("健康证到期提醒");
            sysNotice.setNoticeStatus("0");
            sysNotice.setCreateBy("system");
            sysNotice.setCreatedTime(new Date());
            StringBuffer sb = new StringBuffer();
            sb.append("[持证人]").append(vo.getHolderName()).append(" ");
            sb.append("健康证即将到期，请及时更换");
            sysNotice.setNoticeContent(sb.toString());
            sysNoticeDao.insert(sysNotice);
        }
    }

}
